var Zan = require('../../components/zanui/index');
var utils = require('../../utils/utils');
var app = getApp();

Page(Object.assign({}, Zan.TopTips, {
    data: {
        addShow: false,   //控制添加地址页面的显示和隐藏
        // region: ['广东省', '深圳市', '龙岗区']
        select: false,  //控制是否‘修改默认地址可改设为当前货地址’
        addressList: [],
    },
    onLoad: function (options) {
        var that = this;
        if (options.add) {
            that.setData({
                addShow: true,
                add: options.add
            })
            utils.setNavigationBarTitle('添加地址');
        }
        if (options.sel) {
            that.setData({
                selAdd: true
            })
            utils.setNavigationBarTitle('选择地址');
        }
        this.getAddressList();
    },


    // 获取地址信息
    getAddressList: function () {
        var that = this;
        var httpObj = {
            url: "/userother/addresslist",
            data: {
                who: app.globalData.who,
                userid: app.globalData.userid
            },
            method: 'POST',
            success: function (res) {
                if (res.data.status == 0 ) {
                    that.setData({
                        addressList: []
                    })
                    app.globalData.address = null;
                } else {
                    that.setData({
                        addressList: res.data.address_list
                    })
                }
            },
            fail: function (erorr) {
                that.alert('请求失败' + erorr);
            }
        };
        utils.http(httpObj);
    },


    //设置默认地址
    setDefault: function (e) {
        console.log('设置默认地址 e');
        console.log(e.currentTarget.dataset.id);
        var that = this;
        var httpObj = {
            url: "userother/addresssetdefault",
            data: {
                who: app.globalData.who,
                userid: app.globalData.userid,
                addressid: e.currentTarget.dataset.id,
                act: "default"
            },
            method: "GET",
            success: function (res) {
                // wx.showToast({
                //     title: '设置成功',
                //     icon: 'success'
                // });
                that.getAddressList();
            },
            fail: function (erorr) {
                wx.showModal({
                    title: "设置失败",
                    content: erorr
                })
            }
        };
        utils.http(httpObj);
    },

    //删除地址
    delect: function (e) {
        var that = this;
        var httpObj = {
            url: "userother/addresssetdefault",
            data: {
                who: app.globalData.who,
                userid: app.globalData.userid,
                addressid: e.currentTarget.dataset.id,
                act: "del"
            },
            method: "GET",
            success: function (res) {
                wx.showToast({
                    title: '删除成功',
                    duration: 800,
                    success: function () {
                        that.getAddressList();
                    }
                })
            },
            fail: function (error) {
                wx.showToast({
                    title: '删除失败',
                    content: error,
                    duration: 800,
                    success: function () {
                        that.getAddressList();
                    }
                })
            }
        };

        wx.showModal({
            content: "确定删除此地址？",
            success: function (res) {
                if (res.confirm) {
                    utils.http(httpObj);
                }
            },
            fail: function (erorr) {
                wx.showModal({
                    title: "删除失败",
                    content: erorr
                })
            }
        })
    },


    /**
     * 选择地址
     */
    selAdd: function (e) {
        app.globalData.address = this.data.addressList[e.currentTarget.dataset.index];
        wx.navigateBack();
    },


    //点击添加地址，添加地址页面显示
    add: function () {
        utils.setNavigationBarTitle('添加地址');
        this.setData({
            addShow: true,
            select: false
        })
    },

    //点击关闭，添加地址页面隐藏
    close: function () {
        utils.setNavigationBarTitle('收货地址管理');
        this.setData({
            addShow: false
        })
    },


    //获取input的值
    bindName: function (e) {
        this.setData({
            userName: e.detail.value
        })
    },
    bindTel: function (e) {
        this.setData({
            userTel: e.detail.value
        })
    },
    bindAdd: function (e) {
        this.setData({
            address: e.detail.value
        })
    },

    //打开地图读取当前定位
    orientation: function () {
        var that = this;
        wx.chooseLocation({
            success: function (res) {
                console.log(res);
                that.setData({
                    address: res.address + res.name
                })
            },
            cancel: function () {

            },
        })
    },

    /**
     * 变换省市区
     * @param e
     */
    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            region: e.detail.value
        })
    },

    //读取用户微信上的收获地址
    wechatAdd: function () {
        var that = this;
        wx.chooseAddress({
            success: function (res) {
                that.setData({
                    userName: res.userName,
                    userTel: res.telNumber,
                    //region: [res.provinceName, res.cityName, res.countyName],
                    address: res.cityName + res.countyName + res.detailInfo
                })
            }
        })
    },


    //确定添加地址，提交地址信息
    save: function () {
        var that = this;
        //var address = this.data.region[0] + this.data.region[1] + this.data.region[2] + this.data.address;
        var data = {
            who: app.globalData.who,
            userName: that.data.userName,
            userTel: that.data.userTel,
            address: this.data.address,
            userid: app.globalData.userid
        };
        if (!data.userName) {
            this.showZanTopTips('请填写姓名');
            return;
        }
        if (!data.userTel) {
            this.showZanTopTips('请填写手机号码');
            return;
        }
        if (!this.data.address) {
            this.showZanTopTips('请填写详细地址');
            return;
        }
        wx.request({
            url: app.globalData.commonURL + "/userother/addresssave",
            data: data,
            method: 'POST',
            header: {
                // 'content-type': 'application/json'
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                wx.showToast({
                    title: '添加成功',
                    icon: 'success'
                });
                if (that.data.add) {
                    app.globalData.address = {
                        username: data.userName,
                        tel: data.userTel,
                        address: data.address
                    }
                    wx.navigateBack();
                }
                that.close();
                that.getAddressList();
                that.setData({
                    userName: '',
                    userTel: '',
                    address: ''
                })
            },
            fail: function (erorr) {
                that.alert('请求失败' + erorr);
            }
        })
    },
    //监听下拉
    onPullDownRefresh:function(){
        console.log("下拉刷新");
        wx.stopPullDownRefresh();
    }
}));